package com.emns.service.shijie;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emns.dao.LanmuZiyuanMapper;
import com.emns.dao.ShiJieMapper;
import com.emns.pojo.LanmuZiyuan;
import com.emns.pojo.ShiJie;
import common.DateUtils;
import common.PageInfo;

@Service
public class ShijieService {
	@Autowired
	ShiJieMapper sjm;

	@Autowired
	LanmuZiyuanMapper lzym;

	public int insert(ShiJie shijie) {
		return sjm.insert(shijie);
	}

	public void update(ShiJie shijie) {
		sjm.updateByPrimaryKeySelective(shijie);
	}

	public ShiJie getShijieById(Integer id) {
		return sjm.selectByPrimaryKey(id);
	}

	public Map<Integer, Boolean> getLanmuZiyuanById(Integer id) {
		Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
		for (LanmuZiyuan zy : lzym.selectByZid(id)) {
			map.put(zy.getSecondLanmuId(), true);
		}
		return map;
	}

	public void delLanmuZiyuanById(Integer id) {
		lzym.deleteZylmByZid(id);
	}

	public PageInfo<ShiJie> queryAllVideo(int pageNo, String title, String startDate, String endDate) {
		Date sDate = null;
		Date eDate = null;
		if (title != null && !"".equals(title))
			title = "%" + title + "%";
		else
			title = null;
		if ((startDate != null && !"".equals(startDate)) && (endDate != null && !"".equals(endDate))) {
			startDate = startDate + " 00:00:00";
			sDate = DateUtils.parseDate(startDate, "yyyy-MM-dd HH:mm:ss");
			endDate = endDate + " 23:59:59";
			eDate = DateUtils.parseDate(endDate, "yyyy-MM-dd HH:mm:ss");
		} else {
			startDate = null;
			endDate = null;
		}
		PageInfo<ShiJie> page = new PageInfo<ShiJie>();
		page.setPageNum(pageNo);
		page.setPageCount(sjm.getRecordSize(title, sDate, eDate));
		if (pageNo > page.getPageTotal()) {
			pageNo = page.getPageTotal();
			if (pageNo == 0)
				pageNo = 1;
		}
		int startIndex = (pageNo - 1) * page.getPageSize();
		page.setDataArray(sjm.getRecords(title, sDate, eDate, startIndex, page.getPageSize()));
		return page;
	}

	public void delete(Integer[] ids) {
		for (Integer id : ids) {
			if (id != null) {
				ShiJie shijie = new ShiJie();
				shijie.setShijieId(id);
				shijie.setState(1);
				sjm.updateByPrimaryKeySelective(shijie);
			}
		}
	}

	/**
	 * 添加资源栏目映射
	 * 
	 * @param lmzy
	 * @return
	 */
	public int addLanmuZiyuan(LanmuZiyuan lmzy) {
		return lzym.insert(lmzy);
	}

}
